<?php
class Customer_Model_BalanceHistory
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Customer_Model_DbTable_BalanceHistory();
	}
	
	/**
	*Create A Record  
	*@param rowSet $row 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row 
	*@param int $cbh_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto('cbh_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $cbh_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('cbh_id = ?', $id); 
		return $this->_table->delete($where);		
	}
	
    public function getByCondition($condition,$type='*',$orderby="cbh_id",$pageSize=20,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where(" customer_id =?",$condition["customer_id"]);
        }
		if(isset($condition["cbh_type"]) && $condition["cbh_type"]!=''){
            $select->where(" cbh_type =?",$condition["cbh_type"]);
        }
		if(isset($condition["cbh_op_type"]) && $condition["cbh_op_type"]!=''){
            $select->where(" cbh_op_type =?",$condition["cbh_op_type"]);
        }
		if(isset($condition["cbh_class"]) && $condition["cbh_class"]!=''){
            $select->where(" cbh_class =?",$condition["cbh_class"]);
        }
		if(isset($condition["user_id"]) && $condition["user_id"]!=''){
            $select->where(" user_id =?",$condition["user_id"]);
        }
        if(isset($condition["cbh_refer_id"]) && $condition["cbh_refer_id"]!=''){
            $select->where(" cbh_refer_id =?",$condition["cbh_refer_id"]);
        }
        if(isset($condition["dateFrom"]) && $condition["dateFrom"]!=''){
            $select->where(" cbh_add_time >?",$condition["dateFrom"].' 00:00:00');
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"]!=''){
            $select->where(" cbh_add_time <?",$condition["dateTo"].' 23:59:59');
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}	
	
}